<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Model\CouponCategory;

/**
 * 折扣券分类 DAO
 *
 * @author 刘兴永(aile8880@qq.com)
 * @package App\Dao
 */
class CouponCategoryDao extends Dao
{
    /**
     * 获取优惠券分类
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function getCategoryList(array $map = [])
    {
        return CouponCategory::query()->where(function ($query) use ($map) {
            return queryWhere($query, $map);
        })->where('status', 1)->orderByDesc('sort')->get();
    }

    /**
     * 获取分类信息
     *
     * @param int $id
     * @param bool $abnormal
     * @return mixed
     */
    public function first(int $id, bool $abnormal = true)
    {
        $result = CouponCategory::query()->where('id', $id)->where('status', 1)->first();

        if (!$result && $abnormal == true) {
            $this->error('COUPON_CATEGORY.NOT_FOUND');
        }

        return $result;
    }
}